/* 
 * Copyright 2011 QDSS.org. All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.qdss.oapi4j.apis.paipai;

import org.apache.http.Header;
import org.qdss.oapi4j.Result;
import org.qdss.oapi4j.base.BaseResult;

/**
 * 拍拍结果集
 * 
 * @author <a href="mailto:zhaofang123@gmail.com">Zhao Fangfang</a>
 * @date 2011-3-20
 * @version $Id: PaipaiResult.java 16 2011-04-03 09:59:57Z zhaofang123@gmail.com $
 */
public class PaipaiResult<T> implements Result<T> {

	private BaseResult<T> delegate;
	
	private Integer errorCode;
	private String errorMessage;
	
	/**
	 * @param baseResult
	 */
	public PaipaiResult(BaseResult<T> baseResult) {
		this.delegate = baseResult;
	}
	
	/**
	 * @return
	 */
	public BaseResult<T> getDelegate() {
		return delegate;
	}
	
	public int getInvokeTime() {
		return delegate.getInvokeTime();
	}
	
	public Header[] getHeaders() {
		return delegate.getHeaders();
	}
	
	public boolean hasError() {
		if (errorCode == null) {
			errorCode = getIntValue("//errorCode");
			errorCode = errorCode == null ? 0 : errorCode;
		}
		return errorCode > 0;
	}

	public String getError() {
		if (!hasError()) {
			return null;
		}
		
		if (errorMessage == null) {
			errorMessage = getStringValue("//errorMessage");
			errorMessage = errorCode + "#" + errorMessage;
		}
		return errorMessage;
	}
	
	public T rootValue() {
		return delegate.rootValue();
	}
	
	public T nodeValue(String path) {
		return delegate.nodeValue(path);
	}
	
	public boolean hasValue(String path) {
		return delegate.hasValue(path);
	}

	public String getStringValue(String path) {
		return delegate.getStringValue(path);
	}
	
	public Integer getIntValue(String path) {
		return delegate.getIntValue(path);
	}

	public Long getLongValue(String path) {
		return delegate.getLongValue(path);
	}
	
	public Double getDoubleValue(String path) {
		return delegate.getDoubleValue(path);
	}
}
